package cn.getech.data.development.utils

import java.text.{ParsePosition, SimpleDateFormat}
import java.util.Date

class DateUtils {
  final val TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  final val DATE_KEY_FORMAT = new SimpleDateFormat("yyyyMMdd")
  final val DATE_TIME_KEY_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss")

  /**
   * 根据format获取当天时间
   *
   * @param pattern format
   * @return
   */
  def getPatternTime(pattern: String): String = {
    new SimpleDateFormat(pattern).format(new Date)
  }

  /**
   * 获取当天日期 格式:yyyy-MM-dd
   *
   * @return 当天日期
   */
  def getTodayDate: String = {
    TIME_FORMAT.format(new Date)
  }

  def getTodayDateTime: String = {
    DATE_TIME_KEY_FORMAT.format(new Date)
  }

  def formatStringToDate(dateStr: String): Date = {
    TIME_FORMAT.parse(dateStr)
  }

  def customFormat2String(format: String, dataStr: String) = {
    val formatter = new SimpleDateFormat(format)
    val date = formatter.parse(dataStr)
    val str = formatter.format(date)
    str
  }

  /**
   * 格式化日期 date string转yyyyMMdd日期,没有-间隔
   *
   * @param date "yyyy-MM-dd HH:mm:ss"
   * @return yyyyMMdd
   */
  def formatKeyDate(date: String): String = DATE_KEY_FORMAT.format(TIME_FORMAT.parse(date))

  /**
   * 格式化日期 date string转yyyyMM日期,没有-间隔
   */
  def getYearMonthDate(): String = {
    this.getTodayDate.substring(0, 10).replace("-", "")
  }
}
